package com.cskaoyan.service;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.MarketUserExample;
import com.cskaoyan.bean.bo.WxCommentListBo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.WxCommentCountVo;
import com.cskaoyan.bean.vo.WxCommentListVo;
import com.cskaoyan.bean.vo.wx.comment.list.UserInfo;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.utils.StaticFilePath;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: JianhuaZeng
 * @Date: 2023/1/2 10:54
 */
@Service
public class WxCommentServiceImpl implements WxCommentService{

    @Autowired
    MarketCommentMapper commentMapper;

    @Autowired
    MarketUserMapper userMapper;

    /**
     * @description: 根据条件查找评论
     * @param: commentListBo
     * @return: com.cskaoyan.bean.common.CommonData
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    public CommonData list(WxCommentListBo commentListBo){
        Integer valueId = commentListBo.getValueId();
        Byte type = (byte) ((int) commentListBo.getType());
        Integer limit = commentListBo.getLimit();
        Integer page = commentListBo.getPage();
        Integer showType = commentListBo.getShowType();

        PageHelper.startPage(page,limit);
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        if (showType==1){
            // 查找有图片的评论
            criteria.andHasPictureEqualTo(true);
        }
        commentExample.setOrderByClause("add_time");
        List<MarketComment> comments = commentMapper.selectByExample(commentExample);

        List<WxCommentListVo> commentListVos = new ArrayList<>();
        for (MarketComment comment : comments) {
            WxCommentListVo commontListVo = new WxCommentListVo();
            commontListVo.setAddTime(comment.getAddTime());
            commontListVo.setAdminContent(comment.getAdminContent());
            commontListVo.setContent(comment.getContent());
            commontListVo.setPicList(comment.getPicUrls());
            // 根据userId查询用户信息
            MarketUser user = userMapper.selectByPrimaryKey(comment.getUserId());
            String avatarUrl = StaticFilePath.getPicUrl(user.getAvatar());
            commontListVo.setUserInfo(new UserInfo(avatarUrl,user.getNickname()));
            commentListVos.add(commontListVo);
        }

        return CommonData.data(new PageInfo<>(commentListVos));
    }

    /**
     * @description: 查询评论数量
     * @param: valueId
     * @param: type
     * @return: com.cskaoyan.bean.vo.WxCommentCountVo
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    public WxCommentCountVo count(Integer valueId,Byte type){
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        // 查询所有评论数量
        long allConut = commentMapper.countByExample(commentExample);
        criteria.andHasPictureEqualTo(true);
        // 查询有图片的评论
        long hasPicCount = commentMapper.countByExample(commentExample);
        return new WxCommentCountVo(allConut, hasPicCount);
    }

    /**
     * @description: 插入评论记录，并返回自增主键id
     * @param: marketComment
     * @return: java.lang.Integer
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    public Integer post(MarketComment marketComment){
        commentMapper.insertSelective(marketComment);
        return marketComment.getId();
    }
}
